//
//  YPSecondTabBarController.m
//  一个app嵌套多个tabbar
//
//  Created by 赖永鹏 on 2017/2/9.
//  Copyright © 2017年 赖永鹏. All rights reserved.
//mjtabbar

#import "MainTabBarViewController.h"
#import "HomeController.h"
#import "MineController.h"
@interface MainTabBarViewController ()

@end

@implementation MainTabBarViewController
- (instancetype)initWithContext:(NSString *)context {
    
    /**
     * 以下两行代码目的在于手动设置让TabBarItem只显示图标，不显示文字，并让图标垂直居中。
     * 等 效于在 `-tabBarItemsAttributesForController` 方法中不传 `CYLTabBarItemTitle` 字段。
     * 更推荐后一种做法。
     */
    UIEdgeInsets imageInsets = UIEdgeInsetsZero;//UIEdgeInsetsMake(4.5, 0, -4.5, 0);
    UIOffset titlePositionAdjustment = UIOffsetZero; // UIOffsetMake(0, -3.5);
    if (self = [super initWithViewControllers:[self viewControllersForTabBar]
                        tabBarItemsAttributes:[self tabBarItemsAttributesForTabBar]
                                  imageInsets:imageInsets
                      titlePositionAdjustment:titlePositionAdjustment
                                      context:context
               ]) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.modalPresentationStyle = UIModalPresentationFullScreen;
    self.delegate = self;
    [self setUIAppearance];
 
    MJWeakSelf
    [RACObserve(LanguageHelper.shared, language) subscribeNext:^(NSString *language) {
        [weakSelf updateUIAppearance];
    }];
}

- (void)updateUIAppearance{
    
   
    UIViewController *viewController0 = self.viewControllers[0];
    viewController0.tabBarItem.image = [MJUIImageMake(@"icon_home1") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController0.tabBarItem.selectedImage = [MJUIImageMake(@"icon_home2") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController0.tabBarItem.title = MJUILocalizedString(@"Home");
    
    UIViewController *viewController1 = self.viewControllers[1];
    viewController1.tabBarItem.image = [MJUIImageMake(@"icon_me1") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController1.tabBarItem.selectedImage = [MJUIImageMake(@"icon_me2") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    viewController1.tabBarItem.title = MJUILocalizedString(@"Me");

}

- (NSArray *)viewControllersForTabBar {
    HomeController *vc1 = [[HomeController alloc] init];
    MineController *vc2 = [[MineController alloc] init];

    BasicNavigationController *navVC1 = [[BasicNavigationController alloc] initWithRootViewController:vc1];
    BasicNavigationController *navVC2 = [[BasicNavigationController alloc] initWithRootViewController:vc2];
    return @[navVC1, navVC2];
//    return @[navVC2, navVC1];
}

- (void)setUIAppearance{
    
    //self.selectedIndex = 0;
    [self customizeTabBarAppearance:MJThemeStyleDefault];
    
}

/// 在`-setViewControllers:`之前设置TabBar的属性，设置TabBarItem的属性，包括 title、Image、selectedImage。
- (NSArray *)tabBarItemsAttributesForTabBar{
    NSDictionary *dict1 = @{
                            CYLTabBarItemTitle : MJUILocalizedString(@"Home"),
                            CYLTabBarItemImage : @"icon_home1",
                            CYLTabBarItemSelectedImage : @"icon_home2",
                            };
    NSDictionary *dict2 = @{
                            CYLTabBarItemTitle : MJUILocalizedString(@"Me"),
                            CYLTabBarItemImage : @"icon_me1",
                            CYLTabBarItemSelectedImage : @"icon_me2",
                            };
    return @[dict1, dict2];
}

/// 更多TabBar自定义设置：比如：tabBarItem 的选中和不选中文字和背景图片属性、tabbar 背景图片属性
- (void)customizeTabBarAppearance:(MJThemeStyle)mThemeStyle{
    
    UIColor *normalColor = UIColor.mj_grayColor;
    UIColor *selectedColor = UIColor.mj_appColor;
    UIColor *backgroundColor = UIColor.whiteColor;
    UIColor *shadowColor = [UIColor mj_colorWithHexString:@"#E6E6E6"];
    
    if(mThemeStyle == MJThemeStyleTechnology){
        normalColor = UIColor.whiteColor;
        selectedColor = UIColor.mj_appColor;
        backgroundColor = MJUIColorFromHex(0x30426D);
        shadowColor = [UIColor clearColor];
    }
    if(mThemeStyle == MJThemeStyleTCM){
        normalColor = UIColor.mj_grayColor;
        selectedColor = MJUIColorFromHex(0x8F6C40);
        backgroundColor = UIColor.whiteColor;
        shadowColor = [UIColor mj_colorWithHexString:@"#E6E6E6"];
    }

    if (@available(iOS 13.0, *)) {
        // iOS 13以上 设置文字属性
        UITabBarItemAppearance *tabBarItemAppearance = [[UITabBarItemAppearance alloc]init];
        //没选中
        tabBarItemAppearance.normal.titleTextAttributes = @{NSForegroundColorAttributeName : normalColor};
        //选中
        tabBarItemAppearance.selected.titleTextAttributes = @{NSForegroundColorAttributeName : selectedColor};

        UITabBarAppearance *tabBarAppearance = [[UITabBarAppearance alloc]init];
        tabBarAppearance.stackedLayoutAppearance = tabBarItemAppearance;
        [tabBarAppearance configureWithOpaqueBackground];
        tabBarAppearance.backgroundColor = backgroundColor;//背景颜色
        tabBarAppearance.shadowColor = UIColor.clearColor;//隐藏UITabbar顶部分割线，自带的顶部阴影
        self.tabBar.standardAppearance = tabBarAppearance;
        if (@available(iOS 15.0, *)) {
            self.tabBar.scrollEdgeAppearance = tabBarAppearance;
        }
    } else {
        // iOS 13以下
        UITabBarItem *tabBar = [UITabBarItem appearance];
        //没选中
        [tabBar setTitleTextAttributes:@{NSForegroundColorAttributeName:normalColor} forState:UIControlStateNormal];
        //选中
        [tabBar setTitleTextAttributes:@{NSForegroundColorAttributeName:selectedColor} forState:UIControlStateSelected];
        
        [self.tabBar setBackgroundImage:[UIImage mj_imageWithColor:backgroundColor]];

    }
    
    //添加阴影效果
    self.tabBar.layer.shadowColor = shadowColor.CGColor;
    self.tabBar.layer.shadowOffset = CGSizeMake(0, -2);
    self.tabBar.layer.shadowOpacity = 0.8;
   

  
}

@end
